package com.specter.sure.core;

import java.io.Serial;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.specter.utils.StringUtils;

import lombok.Data;

/**
 * Note:用户详细信息，包括用户权限组
 * 
 * @author Liang.Wang
 * @version 2010-11-25
 */
@Data
public class Authorized implements Serializable {
	@Serial
	private static final long serialVersionUID = 8154225047427803380L;
	/** 用户唯一标识 */
	private String useruuid;
	/** 用户部门标识 */
	private String deptuuid;
	/** 用户数据区域 */
	private String areacode;
	/** 用户登录密码 */
	private String password;
	/** 用户权限集合 */
	private Permission[] permissions;

	/**
	 * 构造函数，写数据的唯一入口
	 * 
	 * @param useruuid 用户内部ID，不可为空
	 * @param areacode 用户数据区域，可为空
	 * @param deptuuid 用户登录ID，不可为空
	 */
	public Authorized(String useruuid, String deptuuid, String areacode) {
		if (StringUtils.isBlank(useruuid)) {
			throw new IllegalArgumentException("useruuid must not blank.");
		}
		if (StringUtils.isBlank(deptuuid)) {
			throw new IllegalArgumentException("deptuuid must not blank.");
		}
		if (StringUtils.isBlank(areacode)) {
			throw new IllegalArgumentException("areacode must not blank.");
		}
		this.useruuid = useruuid;
		this.deptuuid = deptuuid;
		this.areacode = areacode;
	}

	/**
	 * 构造函数 通过数据数据Map转换
	 * 
	 * @param fromMap 数据Map
	 */
	public Authorized(Map<?, ?> fromMap) {
		this.useruuid = String.valueOf(fromMap.get("useruuid"));
		this.deptuuid = String.valueOf(fromMap.get("deptuuid"));
		this.areacode = String.valueOf(fromMap.get("areacode"));
	}

	/**
	 * 将对象转换成Map对象
	 * 
	 * @return 用户的map对象
	 */
	public Map<String, String> toMap() {
		Map<String, String> claims = new HashMap<>();
		claims.put("useruuid", this.getUseruuid());
		claims.put("deptuuid", this.getDeptuuid());
		claims.put("areacode", this.getAreacode());
		return claims;
	}
}
